﻿using System.IO;
using System.Windows.Forms;
using NExplorer.AddIns.Samples.Forms;
using System;

namespace NExplorer.AddIns.Samples.Commands
{
	public class AddPrefixCommand : CommandBase
	{
		public AddPrefixCommand()
			: base("Add Prefix")
		{ }

		public override void Execute()
		{
			base.Execute();

			AddPrefixForm form = new AddPrefixForm();

			if (form.ShowDialog() == DialogResult.OK)
			{
				foreach (FileSystemInfo info in ServiceManager.Explorer.SelectedItems)
				{
					try
					{
						if (info is FileInfo)
						{
							FileInfo fileInfo = (FileInfo)info;
							fileInfo.MoveTo(Path.Combine(fileInfo.DirectoryName, form.Prefix + info.Name));
						}
						else if (info is DirectoryInfo)
						{
							DirectoryInfo dirInfo = (DirectoryInfo)info;						
							dirInfo.MoveTo(Path.Combine(dirInfo.Parent.FullName, form.Prefix + info.Name));
						}
					}
					catch (Exception e)
					{
						string text = string.Format("The following error occurred while trying to rename '{0}'. Would you like to skip it and continue?\n\n", info.Name, e.Message);
						if (MessageBox.Show(text, "Rename Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)
						{
							break;
						}
					}
				}
			}
		}
	}
}